package cn.school.web;

import cn.school.service.GradeService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

@WebServlet(name = "studentServlet", urlPatterns = {"/stuReg", "/zhuce", "/addStu"})
public class StudentRegisterServlet extends HttpServlet {
    //int ar[]={1,3,4,5}
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("grades", GradeService.INST.findALL());
        req.getRequestDispatcher("/WEB-INF/jsp/addstudent.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("utf-8");
//
//        String name = req.getParameter("sname");
//        String sex = req.getParameter("sex");
//        String telephone = req.getParameter("telephone");
//        String  wechat = req.getParameter("wechat");//null？
//        String idpic = req.getParameter("idcardpic");

        //
      //Map<String, String[]> params = req.getParameterMap();
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.println("<html>");
        out.println("<body>");
        DiskFileItemFactory df=new DiskFileItemFactory();

        ServletFileUpload up=new ServletFileUpload(df);
        up.setHeaderEncoding("utf-8");
        try {
            List<FileItem> list = up.parseRequest(req);
            for(FileItem f:list){
                if(f.isFormField()){//普通表单域
                    out.print("<h1>"+f.getFieldName()+"="+f.getString("utf-8")+"</h1>");
                }else{
                    if(f.getSize()<1)
                        continue;
                    out.print("文件名是："+f.getName()+",文件"+f.getSize());
                    String path = req.getServletContext().getRealPath("images");
                    File folder=new File(path);
                    if(folder.exists()==false)
                        folder.mkdirs();
                    String name=f.getName();
                    String suffix=name.substring(name.lastIndexOf('.'));
                    String newName= UUID.randomUUID()+suffix;
                    f.write(new File(folder,newName));
                    out.print("<img height='200px' width='150px' src=\"images/"+newName+"\"  />");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //out.println("<h1>IP:" + ip + "</h1>");
//        out.println("<h1>");
//        out.println("sname " + name);
//        out.println("</h1>");
//        out.println("<h1>");
//        out.println("手机号" + telephone);
//        out.println("</h1>");
//        out.println("<h1>");
//        out.println("微信：" + wechat);
//        out.println("</h1>");
//        out.println("<h1>");
//        out.println("idpic：" + idpic);
//        out.println("</h1>");
//        out.println("<h1>");
//        out.println("身份证照片：" + idpic);
//        out.println("</h1>");
//        out.println("<hr size='40'/>");
//
//        for (Map.Entry<String, String[]> x : params.entrySet()) {
//            out.println("<h2>" + x.getKey() + "=" + Arrays.toString(x.getValue()) + "</h2>");
//        }
//        out.println("<div style='color:red'>");
//        ServletInputStream in = req.getInputStream();
//        int ch=0;
//        while ((ch=in.read())!=-1){
//            out.print((char)ch);
//        }
//        out.println("</div>");
        out.println("</body>");
        out.println("</html>");

    }
}
